$(function () {
	var searchParam="";
	var urlParam=location.search;
	if(urlParam){
		searchParam=urlParam;
	}
    $("#jqGrid").Grid({
        url: '../storage/info/list'+searchParam,
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '仓库编码', name: 'code', index: 'code', width: 80},
			{label: '仓库名称', name: 'name', index: 'name', width: 80},
			{label: '仓库分类', name: 'classifyName', index: 'classify_id', width: 80},
			{label: '地址', name: 'address', index: 'address', width: 80},
			{label: '仓位总数', name: 'space', index: 'space', width: 80}]
    });
});
var setting = {
    data: {
        simpleData: {enable: true,idKey:"id",pIdKey:"parentId",rootPId: -1},
        key:{url: "nourl"}
    }
};
var ztree;
var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		storageInfo: {classifyId:null,classifyName:null},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',
            code: '',
            classifyName: '',
            address: ''
		}
	},
	methods: {
		storageClassifyTree: function () {
            openWindow({
                title: "选择分类",
                area: ['300px', '450px'],
                content: jQuery("#storageClassifyLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.storageInfo.classifyId = node[0].id;
                    vm.storageInfo.classifyName = node[0].name;
                    layer.close(index);
                }
            });
        },
		getStorageClassifyTree:function(){
			Ajax.request({
                url: "../storage/classify/queryAll",
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#storageClassifyTree"), setting, r.list);
                    var node = ztree.getNodeByParam("id", vm.storageInfo.classifyId);
                    if (node) {
                        ztree.selectNode(node);
                        vm.storageInfo.classifyName = node.name;
                    }
                }
            });
		},
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.storageInfo = {classifyId:null,classifyName:null};
			vm.getStorageClassifyTree();
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getStorageClassifyTree();
		},
		saveOrUpdate: function (event) {
            var url = vm.storageInfo.id == null ? "../storage/info/save" : "../storage/info/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.storageInfo),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../storage/info/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../storage/info/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.storageInfo = r.storageInfo;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'name': vm.q.name,
                    'code': vm.q.code,
                    'classifyName': vm.q.classifyName,
                    'address': vm.q.address
                },
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: '',
                code:'',
                classifyName: "",
                address: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});